//
//  MessageContentVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/23.
//  Copyright © 2020 tym. All rights reserved.
//

#import "MessageContentVC.h"
#import "FamilyModel.h"
#import "UserModel.h"
#import "MJRefresh.h"
#import "UserMessageService.h"
#import "MessageContentCell.h"
@interface MessageContentVC ()

@property(nonatomic,strong) UserMessageService *messageService;
@property(nonatomic,strong) NSMutableArray *sectionTitleArray;
@property(nonatomic,strong) NSMutableDictionary *dataDic;
@property(nonatomic,weak) IBOutlet UITableView *tableView;
@property(nonatomic,strong) UserMessageModel *messageModel;

@property(nonatomic,assign) NSInteger pageIndex;
@end

@implementation MessageContentVC

-(instancetype)initWithMessageModel:(UserMessageModel*)model{
    self = [super init];
    
    self.messageModel = model;
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    self.view.backgroundColor = rgba(245, 245, 245);
    
    [self addTitle:[_messageModel getTypeStr]];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MessageContentCell class]) bundle:nil] forCellReuseIdentifier:[MessageContentCell identifier]];
    self.pageIndex = 1;
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.pageIndex = 1;
        [self getMessageData];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.pageIndex += 1;
        [self getMessageData];
    }];
    [self getMessageData];
}

-(void)getRedeadMessage{
    NSString *idS=@"";
    for (NSString *titleScc in self.sectionTitleArray) {
        NSMutableArray *array = self.dataDic[titleScc];
        for (UserMessageModel *model in array) {
            idS = [NSString stringWithFormat:@"%@,%ld",idS,model.id];
        }
    }
    
    [self.messageService getReadmessage:[idS substringFromIndex:1] completion:^(LMJBaseResponse *respon) {
        
    }];
}

-(void)getMessageData{
    TYMWeak(self);
    [self.messageService getFindUsermessagesFamilyID:self.messageModel.familyId type:_messageModel.type
                                           pageIndex:self.pageIndex equipmentId:@"" equipmentMsgType:0 completion:^(LMJBaseResponse *respon, NSArray<UserMessageModel *> *mesageArray) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (mesageArray.count < 10) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self.tableView.mj_footer resetNoMoreData];
        }
        if (self.pageIndex == 1) {
            [self.dataDic removeAllObjects];
            [self.sectionTitleArray removeAllObjects];
        }
        for (UserMessageModel *model in mesageArray) {
            if (!model.createTime) {
                continue;
            }
            if (![self.sectionTitleArray containsObject:[model.createTime substringToIndex:10]]) {
                
                [self.sectionTitleArray addObject:[model.createTime substringToIndex:10]];
                
                NSMutableArray *array = [[NSMutableArray alloc] init];
                [array addObject:model];
                [self.dataDic setValue:array forKey:[model.createTime substringToIndex:10]];
            }else{
                NSMutableArray *array = self.dataDic[[model.createTime substringToIndex:10]];
                [array addObject:model];
                [self.dataDic setValue:array forKey:[model.createTime substringToIndex:10]];
            }
        }
        [weakself.tableView reloadData];
        [weakself getRedeadMessage];
    }];
}

-(UserMessageService*)messageService{
    if (_messageService == nil) {
        _messageService = [[UserMessageService alloc] init];
    }
    return _messageService;
}

-(NSMutableDictionary*)dataDic{
    if (_dataDic == nil) {
        _dataDic = [[NSMutableDictionary alloc] init];
    }
    return _dataDic;
}

-(NSMutableArray*)sectionTitleArray{
    if (_sectionTitleArray == nil) {
        _sectionTitleArray = [[NSMutableArray alloc] init];
    }
    return _sectionTitleArray;
}

//MARK: - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sectionTitleArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     NSMutableArray *array = self.dataDic[self.sectionTitleArray[section]];
       return array.count;
}

-(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
    UILabel *title = [TYMLabel lb_text:[self.sectionTitleArray[section] substringWithRange:NSMakeRange(5, 5)] font:Font_Medium(12) color:rgba(74, 74, 74)];
                      title.frame = CGRectMake(ScreenWidth/2-20, 15, 40, 20);
    title.textAlignment = NSTextAlignmentCenter;
    [footView addSubview:title];
 
    UIView *leftLine = [[UIView alloc] init];
    leftLine.backgroundColor = rgba(216, 219, 227);
    leftLine.frame = CGRectMake(10, 25, ScreenWidth/2-40, 0.5);
    [footView addSubview:leftLine];
    
    UIView *rightLine = [[UIView alloc] init];
    rightLine.backgroundColor = rgba(216, 219, 227);
    rightLine.frame = CGRectMake(ScreenWidth/2+30, 25, ScreenWidth/2-40, 0.5);
    [footView addSubview:rightLine];
    return footView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSMutableArray *array = self.dataDic[self.sectionTitleArray[indexPath.section]];
    return [MessageContentCell cellHeightWithModel:array[indexPath.row]];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MessageContentCell *cell = [tableView dequeueReusableCellWithIdentifier:[MessageContentCell identifier]];
    NSMutableArray *array = self.dataDic[self.sectionTitleArray[indexPath.section]];
    if(array.count){
        
        cell.userModel = array[indexPath.row];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

@end
